from django import forms
from course.models import Course
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import transaction
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from plan.models import Plan, PlannedCourse
from student.forms import RegistrationForm, ProfileForm
from student.models import Student, DegreeRequirements
from schedule.models import Schedule

@transaction.commit_on_success
def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            print form.cleaned_data

            username = form.cleaned_data['username']
            email = username + "@engmail.uwaterloo.ca"
            password = form.cleaned_data['password1']
            student_id = form.cleaned_data['student_id']
            first = form.cleaned_data['first_name']
            last = form.cleaned_data['last_name']
            expected_grad = form.cleaned_data['expected_graduation']

            u = User.objects.create_user(
                username=username,
                email=email,
                password=password
            )

            u.is_active = True
            u.save()

            deg_req = DegreeRequirements.objects.get(is_default=True)
            sched = Schedule.objects.create()

            s = Student(
                user = u,
                uw_id = student_id,
                degree_requirement = deg_req,
                first_name = first,
                last_name = last,
                expected_graduation = expected_grad,
                schedule = sched,
            )

            s.save()

            Plan.initialize_plan(s)

            user = authenticate(username=username, password=password)
            login(request, user)

            return HttpResponseRedirect(reverse('index'))
    else:
        form = RegistrationForm()

    return render_to_response(
        "registration/register.html",
        {'form': form},
        context_instance=RequestContext(request))

def edit_profile(request):
    u = request.user
    s = Student.objects.get(user = u)

    if request.method == 'POST':
        form = ProfileForm(request.POST)
        if form.is_valid():
            print form.cleaned_data

            email = form.cleaned_data['email']
            student_id = form.cleaned_data['student_id']
            first = form.cleaned_data['first_name']
            last = form.cleaned_data['last_name']
            expected_grad = form.cleaned_data['expected_graduation']

            u.email = email
            u.save()

            s.first = first
            s.uw_id = student_id
            s.last = last
            s.expected_graduation = expected_grad
            s.save()
            return HttpResponseRedirect(reverse('edit_profile'))
    else:
        data = {
            'email': u.email,
            'student_id': s.uw_id,
            'first_name': s.first_name,
            'last_name': s.last_name,
            'expected_graduation': s.expected_graduation,
        }

        form = ProfileForm(data)

    return render_to_response(
        "student/profile.html",
        {'form': form},
        context_instance=RequestContext(request))

